package quotation.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Optional;


@RestControllerAdvice
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {


    @Autowired
    private RedisTemplate redisTemplate;

    public static final String TOKEN_KEY = "token:";


    private static final List<String> WHITE_LIST = Collections.singletonList(
            "/quotation/api/**"
    );


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {

        //redisTemplate.opsForValue().get(111);
        //request.setAttribute("starttime", System.currentTimeMillis());
        //AntPathMatcher antPathMatcher = SpringUtil.getBean("antPathMatcher");
        //String method = request.getMethod();
        //if (method.equals(HttpMethod.OPTIONS.name())) {
        //    return true;
        //}
        //String uri = request.getRequestURI();
        //log.error("*********uri:**********" + uri);
        //for (String s : WHITE_LIST) {
        //    if (antPathMatcher.match(s, uri)) {
        //        return true;
        //    }
        //}
        //
        //String token = request.getHeader("token");
        //
        //if (StrUtil.isEmpty(token)) {
        //    log.error("*********token:{},msg:用户凭证不存在**********", token);
        //    throw new BusinessException(400, "用户未登录");
        //}
        //
        //if (stringRedisTemplate == null) {
        //    stringRedisTemplate = SpringUtil.getBean("stringRedisTemplate");
        //}
        //
        //
        //try {
        //    Claims claims = JwtUtil.getClaims(token);
        //
        //    Object userId = claims.get("userId");
        //    Object type = claims.get("type");
        //    Object accountName = claims.get("accountName");
        //
        //    String s = stringRedisTemplate.opsForValue().get(TOKEN_KEY + userId);
        //    if (StringUtils.isEmpty(s)){
        //        s = stringRedisTemplate.opsForValue().get(userId + "guest");
        //        userId = "111";
        //    }
        //    if (StringUtils.isEmpty(s)) {
        //        log.error("*********token:{},msg:用户凭证已失效**********", token);
        //        throw new BusinessException(400, "用户凭证已失效,请重新登陆");
        //    }
        //
        //    LoginInfoUtil.getInstance().add(new LoginInfo(
        //            Long.valueOf(userId.toString()),
        //            Integer.valueOf(type.toString()),
        //            ObjectUtil.isNotEmpty(accountName) ? accountName.toString() : "旅行者"
        //    ));
        //} catch (Exception e) {
        //    log.error("登陆鉴权异常:{}", e);
        //    throw new BusinessException(e.getMessage());
        //}


        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        Long starttime = Optional.ofNullable(request.getAttribute("starttime"))
                .filter(attr -> attr instanceof Long)
                .map(attr -> (Long) attr)
                .orElse(0L); // 提供默认值
        log.info("请求耗时:::::::::::::::>" + (System.currentTimeMillis() - starttime));
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        log.info("********************afterCompletion****************");
    }


}
